from datetime import datetime

from django.db import models
from django.contrib.auth.models import AbstractUser  # 继承django 自带的user表的结构


# Create your models here.


class UserProfile(AbstractUser):
    """
    继承 django 自带的 user 表的结构， 并添加 新的字段
    """
    nick_name = models.CharField(max_length=50, verbose_name="昵称", default="")
    birthday = models.DateField(verbose_name="生日", null=True, blank=True)  # 允许为空 null, blank = True
    gender = models.CharField(choices=(("male", "男"), ("female", "女")), default="female", max_length=6)
    address = models.CharField(max_length=100, default="")
    mobile = models.CharField(max_length=11, null=True, blank=True)
    image = models.ImageField(upload_to="image/%Y/%m", default="image/default.png", max_length=100)  # 头像

    class Meta:
        verbose_name = "用户信息"
        verbose_name_plural = verbose_name

    def __unicode__(self):  # 当print user 的时候 会返回 user 的username
        return self.username


class EmailVerifyRecord(models.Model):
    code = models.CharField(max_length=20, verbose_name="验证码")
    email = models.EmailField(max_length=50, verbose_name="邮箱")
    send_type = models.CharField(choices=(("register", "注册"), ("forget", "找回密码")), max_length=10,
                                 verbose_name="验证码类型")
    send_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")

    class Meta:
        verbose_name = "邮箱验证码"
        verbose_name_plural = verbose_name

    def __unicode__(self):
        return "{}({})".format(self.code, self.email)


class Banner(models.Model):
    """
    轮播图
    """
    title = models.CharField(max_length=100, verbose_name="标题")
    image = models.ImageField(upload_to="banner/%Y/%m", verbose_name="轮播图", max_length=100)
    url = models.URLField(max_length=200, verbose_name="访问地址")
    index = models.IntegerField(default=100, verbose_name="顺序")  # 轮播图的顺序
    add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")

    class Meta:
        verbose_name = "轮播图"
        verbose_name_plural = verbose_name

    def __unicode__(self):
        return self.title


